home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / Small Demos / mulmat.icl < prev    next >
Encoding:
Text File  |  1996-01-17  |  2.4 KB  |  84 lines  |  [TEXT/3PRM]

  1. module mulmat
  2.  
  3. /*
  4. Matrix Multiplication.
  5.  
  6. This  program  performs  matrix multiplication  on matrices of  integers.
  7. Lists are used to simulate matrices
  8. The  initial matrices  (Mat1 & Mat2) can have  arbitrary size (Size). The
  9. second matrix is transposed  first, in order to avoid traversing a matrix
  10. by column, which is very inefficient. The result of the program shows the
  11. initial  matrices  and the  resulting matrix. Run  the program  with  the
  12. "Show Constructors" option on (Application Options).
  13. */
  14.  
  15. import StdInt, StdString
  16.  
  17. ::Row        :== [Int]    // A row is a list of integers.
  18. ::Col        :== [Int]    // A column is a list of integers.
  19. ::Mat        :== [Row]    // A matrix is a list of rows.
  20. ::TMat        :== [Col]    // A transposed matrix is a list of columns.
  21. ::Index_new    :== Int        // An index is an integer.
  22.  
  23. Size :== 6    // The size of the matrices.
  24.  
  25. //    The initial matrices
  26.  
  27. Mat1::Mat
  28. Mat1     =    [[  1, 2, 3, 4, 5, 6 ]        // 
  29.             ,[  0, 1, 2, 3, 4, 5 ]        // 
  30.             ,[ -1, 0, 1, 2, 3, 4 ]        //    The product of these matrices:
  31.             ,[ -2,-1, 0, 1, 2, 3 ]        // 
  32.             ,[ -3,-2,-1, 0, 1, 2 ]        // 
  33.             ,[ -4,-3,-2,-1, 0, 1 ]]        //        [ 0 -9  0  5  1  7 ]
  34.                                         //        [ 0 -8 -1  4  1  6 ]
  35. Mat2::Mat                                //        [ 0 -7 -2  3  1  5 ]
  36. Mat2     =     [[  0, 1, 0, 0, 0,-1 ]        //        [ 0 -6 -3  2  1  4 ]
  37.             ,[  1, 0, 1, 1, 0, 1 ]        //        [ 0 -5 -4  1  1  3 ]
  38.             ,[ -1, 0, 1,-1, 0, 0 ]        //         [ 0 -4 -5  0  1  2 ]
  39.             ,[ -1,-1,-1, 0,-1, 0 ]        // 
  40.             ,[  1, 0, 1, 0, 1, 0 ]        // 
  41.             ,[  0,-1,-1, 1, 0, 1 ]]        // 
  42.  
  43.  
  44. //    Multiplying two matrices.
  45.  
  46. MulMat::Mat Mat -> Mat
  47. MulMat m1 m2   =  TMulMat m1 (Transpose m2)
  48.  
  49. TMulMat::Mat TMat  -> Mat
  50. TMulMat [r] m2    =  [ MulRow r m2 ]
  51. TMulMat [r:rs] m2 =  [ MulRow r m2 : TMulMat rs m2 ]
  52.  
  53. MulRow::Row TMat -> Row
  54. MulRow r [c]    =  [ Inprodukt r c ]
  55. MulRow r [c:cs] =  [ Inprodukt r c : MulRow r cs ]
  56.  
  57. Inprodukt::Row Col  -> Int
  58. Inprodukt [] []         =    0
  59. Inprodukt [a:as] [b:bs] =    a * b  +  Inprodukt as bs 
  60.  
  61. //    Transposing a matrix.
  62.  
  63. Transpose::Mat -> TMat
  64. Transpose m   =  Transp m 1
  65.  
  66. Transp::Mat Index_new -> TMat
  67. Transp m i    | i == Size =  [ Column m i ]
  68.                         =  [ Column m i : Transp m (i + 1) ]
  69.  
  70. Column::Mat Index_new -> Col
  71. Column [] i      =  []
  72. Column [r:rs] i  =  [ Select r i : Column rs i ]
  73.  
  74. Select::Row Index_new -> Int
  75. Select [a:as] 1  =  a
  76. Select [a:as] i  =  Select as (i - 1)
  77.  
  78. //    The Start rule: show the initial matrices and their product.
  79.  
  80. Start::(Mat,String,Mat,String,Mat)
  81. Start     = (m1,"\ntimes\n",m2,"\nbecomes\n",MulMat m1 m2)
  82. where 
  83.       m1    = Mat1; m2 = Mat2
  84.